package com.nicolasprof.profgallery.preloader {
	import gs.TweenLite;
	import gs.easing.*;

	import com.nicolasprof.profgallery.Engine;

	import flash.display.*;

	/**
	 * @author Nicolas Prof
	 */
	public class CircleLoader extends Preloader {

		private var _pivot : Sprite;
		private var _time : Number;
		
		public function CircleLoader() {
			super();
		}

		override public function initiate() : void {
			//INIT
			var config : XML = Engine.CONFIG_XML;
			_pivot = new Sprite();
			
			_time = config.preloaderCircle.@time;
			var radius: Number = config.preloaderCircle.@radius;
			var pipRadius:Number = config.preloaderCircle.@pipRadius;
			var pips: int = config.preloaderCircle.@pips;
			var fillColor : uint = config.preloaderCircle.@mainColor;
			var step: Number = 360 / pips;
			var i:int;
			
			//DISPLAY LIST
			addChild(_pivot);
			
			//LISTENERS
			
			//ACTIONS
			_pivot.filters = [SHADOW];
			
			for(i = 0; i < pips; i++){
				var pip : Shape = new Shape();
				pip.graphics.beginFill(fillColor);
				pip.graphics.drawCircle(0, 0, pipRadius);
				pip.graphics.endFill();
				pip.x = radius * Math.sin(i * step * (Math.PI / 180));
				pip.y = radius * Math.cos(i * step * (Math.PI / 180));
				_pivot.addChild(pip);
				
			}
			
			rotate();
		}
		
		private function rotate() : void {
			_pivot.rotation = 0;
			TweenLite.to(_pivot, _time, {rotation:360, ease:Linear.easeNone, onComplete:rotate});
		}

		override public function toggle(state : Boolean, speed : Number = 0.5) : void {
			super.toggle(state, speed);
			if(state){
				rotate();
			}else {
				TweenLite.killTweensOf(_pivot);
			}
		}

		/**
		 * GETTERS AND SETTERS
		 */
		override public function set progress(ratio : Number) : void {
			//Nothing
		}
	}
}
